<?php
/**
 * Created by PhpStorm.
 * User: Yixiao Chen
 * Date: 2015/5/29 0029
 * Time: 下午 3:15
 */

namespace Appstore\Model;


use Think\Model;

class AppstorePriceModel extends Model
{

    public function getPriceFilter($version_id,$uid =0){
        empty($uid) && $uid = get_uid();
        $role_id = get_role_id($uid);
        $version = D('AppstoreVersion')->getVersion($version_id);
        $return = array();
        $price = $this->where(array('version_id'=>$version_id,'pid'=>0))->order('id asc')->select();
        foreach($price as $key=>&$val){
            $child = $this->where(array('version_id'=>$version_id,'pid'=>$val['id']))->order('id asc')->select();
            $val['model'] = 'price';
            array_push($return,$val);
            foreach($child as $k=>&$v){
                if($v['value'] == $role_id){
                    $v['model'] = 'price';
                    array_push($return,$v);
                }
            }
            $history = D('AppstorePriceMax')->where(array('score_id' => $val['value'], 'uid' => $uid, 'goods_id' => $version['goods_id']))->find();
            if($history){
                $history['model'] = 'max';
                $history['pid'] = $val['id'];
                $history['differ'] =    $val['price'] - $history['max_price'];
                array_push($return,$history);
            }
            unset($v);
        }
        unset($val);
        return $return;
    }


    public function getById($id){
        $price = $this->where(array('status'=>1,'id'=>$id))->find();
        return $price;
    }




}